home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / lib / getdate.y < prev    next >
Encoding:
Lex Description  |  1991-08-19  |  21.2 KB  |  918 lines

  1. %{
  2. /* $Revision: 2.1 $
  3. **
  4. **  Originally written by Steven M. Bellovin <smb@research.att.com> while
  5. **  at the University of North Carolina at Chapel Hill.  Later tweaked by
  6. **  a couple of people on Usenet.  Completely overhauled by Rich $alz
  7. **  <rsalz@bbn.com> and Jim Berets <jberets@bbn.com> in August, 1990;
  8. **  send any email to Rich.
  9. **
  10. **  This grammar has eight shift/reduce conflicts.
  11. **
  12. **  This code is in the public domain and has no copyright.
  13. */
  14. /* SUPPRESS 287 on yaccpar_sccsid *//* Unusd static variable */
  15. /* SUPPRESS 288 on yyerrlab *//* Label unused */
  16.  
  17. #ifdef __GNUC__
  18. #define alloca __builtin_alloca
  19. #else
  20. #ifdef sparc
  21. #include <alloca.h>
  22. #else
  23. #ifdef _AIX /* for Bison */
  24.  #pragma alloca
  25. #else
  26. char *alloca ();
  27. #endif
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <ctype.h>
  33.  
  34. #if    defined(vms)
  35. #include <types.h>
  36. #include <time.h>
  37. #else
  38. #include <sys/types.h>
  39. #if    defined(USG) || defined(FTIME_MISSING)
  40. /*
  41. **  If you need to do a tzset() call to set the
  42. **  timezone, and don't have ftime().
  43. */
  44. struct timeb {
  45.     time_t        time;        /* Seconds since the epoch    */
  46.     unsigned short    millitm;    /* Field not used        */
  47.     short        timezone;
  48.     short        dstflag;    /* Field not used        */
  49. };
  50. #else
  51. #include <sys/timeb.h>
  52. #endif    /* defined(USG) || defined(FTIME_MISSING) */
  53. #if    defined(BSD4_2) || defined(BSD4_1C)
  54. #include <sys/time.h>
  55. #else
  56. #include <time.h>
  57. #endif    /* defined(BSD4_2) */
  58. #endif    /* defined(vms) */
  59.  
  60. #if defined (STDC_HEADERS) || defined (USG)
  61. #include <string.h>
  62. #endif
  63.  
  64. extern struct tm    *localtime();
  65.  
  66. #define yyparse getdate_yyparse
  67. #define yylex getdate_yylex
  68. #define yyerror getdate_yyerror
  69.  
  70. #if    !defined(lint) && !defined(SABER)
  71. static char RCS[] =
  72.     "$Header: str2date.y,v 2.1 90/09/06 08:15:06 cronan Exp $";
  73. #endif    /* !defined(lint) && !defined(SABER) */
  74.  
  75.  
  76. #define EPOCH        1970
  77. #define HOUR(x)        ((time_t)(x) * 60)
  78. #define SECSPERDAY    (24L * 60L * 60L)
  79.  
  80.  
  81. /*
  82. **  An entry in the lexical lookup table.
  83. */
  84. typedef struct _TABLE {
  85.     char    *name;
  86.     int        type;
  87.     time_t    value;
  88. } TABLE;
  89.  
  90.  
  91. /*
  92. **  Daylight-savings mode:  on, off, or not yet known.
  93. */
  94. typedef enum _DSTMODE {
  95.     DSTon, DSToff, DSTmaybe
  96. } DSTMODE;
  97.  
  98. /*
  99. **  Meridian:  am, pm, or 24-hour style.
  100. */
  101. typedef enum _MERIDIAN {
  102.     MERam, MERpm, MER24
  103. } MERIDIAN;
  104.  
  105.  
  106. /*
  107. **  Global variables.  We could get rid of most of these by using a good
  108. **  union as the yacc stack.  (This routine was originally written before
  109. **  yacc had the %union construct.)  Maybe someday; right now we only use
  110. **  the %union very rarely.
  111. */
  112. static char    *yyInput;
  113. static DSTMODE    yyDSTmode;
  114. static time_t    yyDayOrdinal;
  115. static time_t    yyDayNumber;
  116. static int    yyHaveDate;
  117. static int    yyHaveDay;
  118. static int    yyHaveRel;
  119. static int    yyHaveTime;
  120. static int    yyHaveZone;
  121. static time_t    yyTimezone;
  122. static time_t    yyDay;
  123. static time_t    yyHour;
  124. static time_t    yyMinutes;
  125. static time_t    yyMonth;
  126. static time_t    yySeconds;
  127. static time_t    yyYear;
  128. static MERIDIAN    yyMeridian;
  129. static time_t    yyRelMonth;
  130. static time_t    yyRelSeconds;
  131.  
  132. %}
  133.  
  134. %union {
  135.     time_t        Number;
  136.     enum _MERIDIAN    Meridian;
  137. }
  138.  
  139. %token    tAGO tDAY tDAYZONE tID tMERIDIAN tMINUTE_UNIT tMONTH tMONTH_UNIT
  140. %token    tSEC_UNIT tSNUMBER tUNUMBER tZONE tDST
  141.  
  142. %type    <Number>    tDAY tDAYZONE tMINUTE_UNIT tMONTH tMONTH_UNIT
  143. %type    <Number>    tSEC_UNIT tSNUMBER tUNUMBER tZONE
  144. %type    <Meridian>    tMERIDIAN o_merid
  145.  
  146. %%
  147.  
  148. spec    : /* NULL */
  149.     | spec item
  150.     ;
  151.  
  152. item    : time {
  153.         yyHaveTime++;
  154.     }
  155.     | zone {
  156.         yyHaveZone++;
  157.     }
  158.     | date {
  159.         yyHaveDate++;
  160.     }
  161.     | day {
  162.         yyHaveDay++;
  163.     }
  164.     | rel {
  165.         yyHaveRel++;
  166.     }
  167.     | number
  168.     ;
  169.  
  170. time    : tUNUMBER tMERIDIAN {
  171.         yyHour = $1;
  172.         yyMinutes = 0;
  173.         yySeconds = 0;
  174.         yyMeridian = $2;
  175.     }
  176.     | tUNUMBER ':' tUNUMBER o_merid {
  177.         yyHour = $1;
  178.         yyMinutes = $3;
  179.         yySeconds = 0;
  180.         yyMeridian = $4;
  181.     }
  182.     | tUNUMBER ':' tUNUMBER tSNUMBER {
  183.         yyHour = $1;
  184.         yyMinutes = $3;
  185.         yyMeridian = MER24;
  186.         yyDSTmode = DSToff;
  187.         yyTimezone = - ($4 % 100 + ($4 / 100) * 60);
  188.     }
  189.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER o_merid {
  190.         yyHour = $1;
  191.         yyMinutes = $3;
  192.         yySeconds = $5;
  193.         yyMeridian = $6;
  194.     }
  195.     | tUNUMBER ':' tUNUMBER ':' tUNUMBER tSNUMBER {
  196.         yyHour = $1;
  197.         yyMinutes = $3;
  198.         yySeconds = $5;
  199.         yyMeridian = MER24;
  200.         yyDSTmode = DSToff;
  201.         yyTimezone = - ($6 % 100 + ($6 / 100) * 60);
  202.     }
  203.     ;
  204.  
  205. zone    : tZONE {
  206.         yyTimezone = $1;
  207.         yyDSTmode = DSToff;
  208.     }
  209.     | tDAYZONE {
  210.         yyTimezone = $1;
  211.         yyDSTmode = DSTon;
  212.     }
  213.     |
  214.       tZONE tDST {
  215.         yyTimezone = $1;
  216.         yyDSTmode = DSTon;
  217.     }
  218.     ;
  219.  
  220. day    : tDAY {
  221.         yyDayOrdinal = 1;
  222.         yyDayNumber = $1;
  223.     }
  224.     | tDAY ',' {
  225.         yyDayOrdinal = 1;
  226.         yyDayNumber = $1;
  227.     }
  228.     | tUNUMBER tDAY {
  229.         yyDayOrdinal = $1;
  230.         yyDayNumber = $2;
  231.     }
  232.     ;
  233.  
  234. date    : tUNUMBER '/' tUNUMBER {
  235.         yyMonth = $1;
  236.         yyDay = $3;
  237.     }
  238.     | tUNUMBER '/' tUNUMBER '/' tUNUMBER {
  239.         yyMonth = $1;
  240.         yyDay = $3;
  241.         yyYear = $5;
  242.     }
  243.     | tMONTH tUNUMBER {
  244.         yyMonth = $1;
  245.         yyDay = $2;
  246.     }
  247.     | tMONTH tUNUMBER ',' tUNUMBER {
  248.         yyMonth = $1;
  249.         yyDay = $2;
  250.         yyYear = $4;
  251.     }
  252.     | tUNUMBER tMONTH {
  253.         yyMonth = $2;
  254.         yyDay = $1;
  255.     }
  256.     | tUNUMBER tMONTH tUNUMBER {
  257.         yyMonth = $2;
  258.         yyDay = $1;
  259.         yyYear = $3;
  260.     }
  261.     ;
  262.  
  263. rel    : relunit tAGO {
  264.         yyRelSeconds = -yyRelSeconds;
  265.         yyRelMonth = -yyRelMonth;
  266.     }
  267.     | relunit
  268.     ;
  269.  
  270. relunit    : tUNUMBER tMINUTE_UNIT {
  271.         yyRelSeconds += $1 * $2 * 60L;
  272.     }
  273.     | tSNUMBER tMINUTE_UNIT {
  274.         yyRelSeconds += $1 * $2 * 60L;
  275.     }
  276.     | tMINUTE_UNIT {
  277.         yyRelSeconds += $1 * 60L;
  278.     }
  279.     | tSNUMBER tSEC_UNIT {
  280.         yyRelSeconds += $1;
  281.     }
  282.     | tUNUMBER tSEC_UNIT {
  283.         yyRelSeconds += $1;
  284.     }
  285.     | tSEC_UNIT {
  286.         yyRelSeconds++;
  287.     }
  288.     | tSNUMBER tMONTH_UNIT {
  289.         yyRelMonth += $1 * $2;
  290.     }
  291.     | tUNUMBER tMONTH_UNIT {
  292.         yyRelMonth += $1 * $2;
  293.     }
  294.     | tMONTH_UNIT {
  295.         yyRelMonth += $1;
  296.     }
  297.     ;
  298.  
  299. number    : tUNUMBER {
  300.         if (yyHaveTime && yyHaveDate && !yyHaveRel)
  301.         yyYear = $1;
  302.         else {
  303.         if($1>10000) {
  304.             time_t date_part;
  305.  
  306.             date_part= $1/10000;
  307.             yyHaveDate++;
  308.             yyDay= (date_part)%100;
  309.             yyMonth= (date_part/100)%100;
  310.             yyYear = date_part/10000;
  311.         } 
  312.             yyHaveTime++;
  313.         if ($1 < 100) {
  314.             yyHour = $1;
  315.             yyMinutes = 0;
  316.         }
  317.         else {
  318.             yyHour = $1 / 100;
  319.             yyMinutes = $1 % 100;
  320.         }
  321.         yySeconds = 0;
  322.         yyMeridian = MER24;
  323.         }
  324.     }
  325.     ;
  326.  
  327. o_merid    : /* NULL */ {
  328.         $$ = MER24;
  329.     }
  330.     | tMERIDIAN {
  331.         $$ = $1;
  332.     }
  333.     ;
  334.  
  335. %%
  336.  
  337. /* Month and day table. */
  338. static TABLE    MonthDayTable[] = {
  339.     { "january",    tMONTH,  1 },
  340.     { "february",    tMONTH,  2 },
  341.     { "march",        tMONTH,  3 },
  342.     { "april",        tMONTH,  4 },
  343.     { "may",        tMONTH,  5 },
  344.     { "june",        tMONTH,  6 },
  345.     { "july",        tMONTH,  7 },
  346.     { "august",        tMONTH,  8 },
  347.     { "september",    tMONTH,  9 },
  348.     { "sept",        tMONTH,  9 },
  349.     { "october",    tMONTH, 10 },
  350.     { "november",    tMONTH, 11 },
  351.     { "december",    tMONTH, 12 },
  352.     { "sunday",        tDAY, 0 },
  353.     { "monday",        tDAY, 1 },
  354.     { "tuesday",    tDAY, 2 },
  355.     { "tues",        tDAY, 2 },
  356.     { "wednesday",    tDAY, 3 },
  357.     { "wednes",        tDAY, 3 },
  358.     { "thursday",    tDAY, 4 },
  359.     { "thur",        tDAY, 4 },
  360.     { "thurs",        tDAY, 4 },
  361.     { "friday",        tDAY, 5 },
  362.     { "saturday",    tDAY, 6 },
  363.     { NULL }
  364. };
  365.  
  366. /* Time units table. */
  367. static TABLE    UnitsTable[] = {
  368.     { "year",        tMONTH_UNIT,    12 },
  369.     { "month",        tMONTH_UNIT,    1 },
  370.     { "fortnight",    tMINUTE_UNIT,    14 * 24 * 60 },
  371.     { "week",        tMINUTE_UNIT,    7 * 24 * 60 },
  372.     { "day",        tMINUTE_UNIT,    1 * 24 * 60 },
  373.     { "hour",        tMINUTE_UNIT,    60 },
  374.     { "minute",        tMINUTE_UNIT,    1 },
  375.     { "min",        tMINUTE_UNIT,    1 },
  376.     { "second",        tSEC_UNIT,    1 },
  377.     { "sec",        tSEC_UNIT,    1 },
  378.     { NULL }
  379. };
  380.  
  381. /* Assorted relative-time words. */
  382. static TABLE    OtherTable[] = {
  383.     { "tomorrow",    tMINUTE_UNIT,    1 * 24 * 60 },
  384.     { "yesterday",    tMINUTE_UNIT,    -1 * 24 * 60 },
  385.     { "today",        tMINUTE_UNIT,    0 },
  386.     { "now",        tMINUTE_UNIT,    0 },
  387.     { "last",        tUNUMBER,    -1 },
  388.     { "this",        tMINUTE_UNIT,    0 },
  389.     { "next",        tUNUMBER,    2 },
  390.     { "first",        tUNUMBER,    1 },
  391. /*  { "second",        tUNUMBER,    2 }, */
  392.     { "third",        tUNUMBER,    3 },
  393.     { "fourth",        tUNUMBER,    4 },
  394.     { "fifth",        tUNUMBER,    5 },
  395.     { "sixth",        tUNUMBER,    6 },
  396.     { "seventh",    tUNUMBER,    7 },
  397.     { "eighth",        tUNUMBER,    8 },
  398.     { "ninth",        tUNUMBER,    9 },
  399.     { "tenth",        tUNUMBER,    10 },
  400.     { "eleventh",    tUNUMBER,    11 },
  401.     { "twelfth",    tUNUMBER,    12 },
  402.     { "ago",        tAGO,    1 },
  403.     { NULL }
  404. };
  405.  
  406. /* The timezone table. */
  407. /* Some of these are commented out because a time_t can't store a float. */
  408. static TABLE    TimezoneTable[] = {
  409.     { "gmt",    tZONE,     HOUR( 0) },    /* Greenwich Mean */
  410.     { "ut",    tZONE,     HOUR( 0) },    /* Universal (Coordinated) */
  411.     { "utc",    tZONE,     HOUR( 0) },
  412.     { "wet",    tZONE,     HOUR( 0) },    /* Western European */
  413.     { "bst",    tDAYZONE,  HOUR( 0) },    /* British Summer */
  414.     { "wat",    tZONE,     HOUR( 1) },    /* West Africa */
  415.     { "at",    tZONE,     HOUR( 2) },    /* Azores */
  416. #if    0
  417.     /* For completeness.  BST is also British Summer, and GST is
  418.      * also Guam Standard. */
  419.     { "bst",    tZONE,     HOUR( 3) },    /* Brazil Standard */
  420.     { "gst",    tZONE,     HOUR( 3) },    /* Greenland Standard */
  421. #endif
  422. #if 0
  423.     { "nft",    tZONE,     HOUR(3.5) },    /* Newfoundland */
  424.     { "nst",    tZONE,     HOUR(3.5) },    /* Newfoundland Standard */
  425.     { "ndt",    tDAYZONE,  HOUR(3.5) },    /* Newfoundland Daylight */
  426. #endif
  427.     { "ast",    tZONE,     HOUR( 4) },    /* Atlantic Standard */
  428.     { "adt",    tDAYZONE,  HOUR( 4) },    /* Atlantic Daylight */
  429.     { "est",    tZONE,     HOUR( 5) },    /* Eastern Standard */
  430.     { "edt",    tDAYZONE,  HOUR( 5) },    /* Eastern Daylight */
  431.     { "cst",    tZONE,     HOUR( 6) },    /* Central Standard */
  432.     { "cdt",    tDAYZONE,  HOUR( 6) },    /* Central Daylight */
  433.     { "mst",    tZONE,     HOUR( 7) },    /* Mountain Standard */
  434.     { "mdt",    tDAYZONE,  HOUR( 7) },    /* Mountain Daylight */
  435.     { "pst",    tZONE,     HOUR( 8) },    /* Pacific Standard */
  436.     { "pdt",    tDAYZONE,  HOUR( 8) },    /* Pacific Daylight */
  437.     { "yst",    tZONE,     HOUR( 9) },    /* Yukon Standard */
  438.     { "ydt",    tDAYZONE,  HOUR( 9) },    /* Yukon Daylight */
  439.     { "hst",    tZONE,     HOUR(10) },    /* Hawaii Standard */
  440.     { "hdt",    tDAYZONE,  HOUR(10) },    /* Hawaii Daylight */
  441.     { "cat",    tZONE,     HOUR(10) },    /* Central Alaska */
  442.     { "ahst",    tZONE,     HOUR(10) },    /* Alaska-Hawaii Standard */
  443.     { "nt",    tZONE,     HOUR(11) },    /* Nome */
  444.     { "idlw",    tZONE,     HOUR(12) },    /* International Date Line West */
  445.     { "cet",    tZONE,     -HOUR(1) },    /* Central European */
  446.     { "met",    tZONE,     -HOUR(1) },    /* Middle European */
  447.     { "mewt",    tZONE,     -HOUR(1) },    /* Middle European Winter */
  448.     { "mest",    tDAYZONE,  -HOUR(1) },    /* Middle European Summer */
  449.     { "swt",    tZONE,     -HOUR(1) },    /* Swedish Winter */
  450.     { "sst",    tDAYZONE,  -HOUR(1) },    /* Swedish Summer */
  451.     { "fwt",    tZONE,     -HOUR(1) },    /* French Winter */
  452.     { "fst",    tDAYZONE,  -HOUR(1) },    /* French Summer */
  453.     { "eet",    tZONE,     -HOUR(2) },    /* Eastern Europe, USSR Zone 1 */
  454.     { "bt",    tZONE,     -HOUR(3) },    /* Baghdad, USSR Zone 2 */
  455. #if 0
  456.     { "it",    tZONE,     -HOUR(3.5) },/* Iran */
  457. #endif
  458.     { "zp4",    tZONE,     -HOUR(4) },    /* USSR Zone 3 */
  459.     { "zp5",    tZONE,     -HOUR(5) },    /* USSR Zone 4 */
  460. #if 0
  461.     { "ist",    tZONE,     -HOUR(5.5) },/* Indian Standard */
  462. #endif
  463.     { "zp6",    tZONE,     -HOUR(6) },    /* USSR Zone 5 */
  464. #if    0
  465.     /* For completeness.  NST is also Newfoundland Stanard, nad SST is
  466.      * also Swedish Summer. */
  467.     { "nst",    tZONE,     -HOUR(6.5) },/* North Sumatra */
  468.     { "sst",    tZONE,     -HOUR(7) },    /* South Sumatra, USSR Zone 6 */
  469. #endif    /* 0 */
  470.     { "wast",    tZONE,     -HOUR(7) },    /* West Australian Standard */
  471.     { "wadt",    tDAYZONE,  -HOUR(7) },    /* West Australian Daylight */
  472. #if 0
  473.     { "jt",    tZONE,     -HOUR(7.5) },/* Java (3pm in Cronusland!) */
  474. #endif
  475.     { "cct",    tZONE,     -HOUR(8) },    /* China Coast, USSR Zone 7 */
  476.     { "jst",    tZONE,     -HOUR(9) },    /* Japan Standard, USSR Zone 8 */
  477. #if 0
  478.     { "cast",    tZONE,     -HOUR(9.5) },/* Central Australian Standard */
  479.     { "cadt",    tDAYZONE,  -HOUR(9.5) },/* Central Australian Daylight */
  480. #endif
  481.     { "east",    tZONE,     -HOUR(10) },    /* Eastern Australian Standard */
  482.     { "eadt",    tDAYZONE,  -HOUR(10) },    /* Eastern Australian Daylight */
  483.     { "gst",    tZONE,     -HOUR(10) },    /* Guam Standard, USSR Zone 9 */
  484.     { "nzt",    tZONE,     -HOUR(12) },    /* New Zealand */
  485.     { "nzst",    tZONE,     -HOUR(12) },    /* New Zealand Standard */
  486.     { "nzdt",    tDAYZONE,  -HOUR(12) },    /* New Zealand Daylight */
  487.     { "idle",    tZONE,     -HOUR(12) },    /* International Date Line East */
  488.     {  NULL  }
  489. };
  490.  
  491. /* Military timezone table. */
  492. static TABLE    MilitaryTable[] = {
  493.     { "a",    tZONE,    HOUR(  1) },
  494.     { "b",    tZONE,    HOUR(  2) },
  495.     { "c",    tZONE,    HOUR(  3) },
  496.     { "d",    tZONE,    HOUR(  4) },
  497.     { "e",    tZONE,    HOUR(  5) },
  498.     { "f",    tZONE,    HOUR(  6) },
  499.     { "g",    tZONE,    HOUR(  7) },
  500.     { "h",    tZONE,    HOUR(  8) },
  501.     { "i",    tZONE,    HOUR(  9) },
  502.     { "k",    tZONE,    HOUR( 10) },
  503.     { "l",    tZONE,    HOUR( 11) },
  504.     { "m",    tZONE,    HOUR( 12) },
  505.     { "n",    tZONE,    HOUR(- 1) },
  506.     { "o",    tZONE,    HOUR(- 2) },
  507.     { "p",    tZONE,    HOUR(- 3) },
  508.     { "q",    tZONE,    HOUR(- 4) },
  509.     { "r",    tZONE,    HOUR(- 5) },
  510.     { "s",    tZONE,    HOUR(- 6) },
  511.     { "t",    tZONE,    HOUR(- 7) },
  512.     { "u",    tZONE,    HOUR(- 8) },
  513.     { "v",    tZONE,    HOUR(- 9) },
  514.     { "w",    tZONE,    HOUR(-10) },
  515.     { "x",    tZONE,    HOUR(-11) },
  516.     { "y",    tZONE,    HOUR(-12) },
  517.     { "z",    tZONE,    HOUR(  0) },
  518.     { NULL }
  519. };
  520.  
  521.  
  522.  
  523.  
  524. /* ARGSUSED */
  525. int
  526. yyerror(s)
  527.     char    *s;
  528. {
  529.   return 0;
  530. }
  531.  
  532.  
  533. static time_t
  534. ToSeconds(Hours, Minutes, Seconds, Meridian)
  535.     time_t    Hours;
  536.     time_t    Minutes;
  537.     time_t    Seconds;
  538.     MERIDIAN    Meridian;
  539. {
  540.     if (Minutes < 0 || Minutes > 59 || Seconds < 0 || Seconds > 59)
  541.     return -1;
  542.     switch (Meridian) {
  543.     case MER24:
  544.     if (Hours < 0 || Hours > 23)
  545.         return -1;
  546.     return (Hours * 60L + Minutes) * 60L + Seconds;
  547.     case MERam:
  548.     if (Hours < 1 || Hours > 12)
  549.         return -1;
  550.     return (Hours * 60L + Minutes) * 60L + Seconds;
  551.     case MERpm:
  552.     if (Hours < 1 || Hours > 12)
  553.         return -1;
  554.     return ((Hours + 12) * 60L + Minutes) * 60L + Seconds;
  555.     }
  556.     /* NOTREACHED */
  557. }
  558.  
  559.  
  560. static time_t
  561. Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode)
  562.     time_t    Month;
  563.     time_t    Day;
  564.     time_t    Year;
  565.     time_t    Hours;
  566.     time_t    Minutes;
  567.     time_t    Seconds;
  568.     MERIDIAN    Meridian;
  569.     DSTMODE    DSTmode;
  570. {
  571.     static int    DaysInMonth[12] = {
  572.     31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  573.     };
  574.     time_t    tod;
  575.     time_t    Julian;
  576.     int        i;
  577.  
  578.     if (Year < 0)
  579.     Year = -Year;
  580.     if (Year < 100)
  581.     Year += 1900;
  582.     DaysInMonth[1] = Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0)
  583.             ? 29 : 28;
  584.     if (Year < EPOCH || Year > 1999
  585.      || Month < 1 || Month > 12
  586.      /* Lint fluff:  "conversion from long may lose accuracy" */
  587.      || Day < 1 || Day > DaysInMonth[(int)--Month])
  588.     return -1;
  589.  
  590.     for (Julian = Day - 1, i = 0; i < Month; i++)
  591.     Julian += DaysInMonth[i];
  592.     for (i = EPOCH; i < Year; i++)
  593.     Julian += 365 + (i % 4 == 0);
  594.     Julian *= SECSPERDAY;
  595.     Julian += yyTimezone * 60L;
  596.     if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
  597.     return -1;
  598.     Julian += tod;
  599.     if (DSTmode == DSTon
  600.      || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
  601.     Julian -= 60 * 60;
  602.     return Julian;
  603. }
  604.  
  605.  
  606. static time_t
  607. DSTcorrect(Start, Future)
  608.     time_t    Start;
  609.     time_t    Future;
  610. {
  611.     time_t    StartDay;
  612.     time_t    FutureDay;
  613.  
  614.     StartDay = (localtime(&Start)->tm_hour + 1) % 24;
  615.     FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
  616.     return (Future - Start) + (StartDay - FutureDay) * 60L * 60L;
  617. }
  618.  
  619.  
  620. static time_t
  621. RelativeDate(Start, DayOrdinal, DayNumber)
  622.     time_t    Start;
  623.     time_t    DayOrdinal;
  624.     time_t    DayNumber;
  625. {
  626.     struct tm    *tm;
  627.     time_t    now;
  628.  
  629.     now = Start;
  630.     tm = localtime(&now);
  631.     now += SECSPERDAY * ((DayNumber - tm->tm_wday + 7) % 7);
  632.     now += 7 * SECSPERDAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
  633.     return DSTcorrect(Start, now);
  634. }
  635.  
  636.  
  637. static time_t
  638. RelativeMonth(Start, RelMonth)
  639.     time_t    Start;
  640.     time_t    RelMonth;
  641. {
  642.     struct tm    *tm;
  643.     time_t    Month;
  644.     time_t    Year;
  645.  
  646.     if (RelMonth == 0)
  647.     return 0;
  648.     tm = localtime(&Start);
  649.     Month = 12 * tm->tm_year + tm->tm_mon + RelMonth;
  650.     Year = Month / 12;
  651.     Month = Month % 12 + 1;
  652.     return DSTcorrect(Start,
  653.         Convert(Month, (time_t)tm->tm_mday, Year,
  654.         (time_t)tm->tm_hour, (time_t)tm->tm_min, (time_t)tm->tm_sec,
  655.         MER24, DSTmaybe));
  656. }
  657.  
  658.  
  659. static int
  660. LookupWord(buff)
  661.     char        *buff;
  662. {
  663.     register char    *p;
  664.     register char    *q;
  665.     register TABLE    *tp;
  666.     int            i;
  667.     int            abbrev;
  668.  
  669.     /* Make it lowercase. */
  670.     for (p = buff; *p; p++)
  671.     if (isupper(*p))
  672.         *p = tolower(*p);
  673.  
  674.     if (strcmp(buff, "am") == 0 || strcmp(buff, "a.m.") == 0) {
  675.     yylval.Meridian = MERam;
  676.     return tMERIDIAN;
  677.     }
  678.     if (strcmp(buff, "pm") == 0 || strcmp(buff, "p.m.") == 0) {
  679.     yylval.Meridian = MERpm;
  680.     return tMERIDIAN;
  681.     }
  682.  
  683.     /* See if we have an abbreviation for a month. */
  684.     if (strlen(buff) == 3)
  685.     abbrev = 1;
  686.     else if (strlen(buff) == 4 && buff[3] == '.') {
  687.     abbrev = 1;
  688.     buff[3] = '\0';
  689.     }
  690.     else
  691.     abbrev = 0;
  692.  
  693.     for (tp = MonthDayTable; tp->name; tp++) {
  694.     if (abbrev) {
  695.         if (strncmp(buff, tp->name, 3) == 0) {
  696.         yylval.Number = tp->value;
  697.         return tp->type;
  698.         }
  699.     }
  700.     else if (strcmp(buff, tp->name) == 0) {
  701.         yylval.Number = tp->value;
  702.         return tp->type;
  703.     }
  704.     }
  705.  
  706.     for (tp = TimezoneTable; tp->name; tp++)
  707.     if (strcmp(buff, tp->name) == 0) {
  708.         yylval.Number = tp->value;
  709.         return tp->type;
  710.     }
  711.  
  712.     if (strcmp(buff, "dst") == 0) 
  713.     return tDST;
  714.  
  715.     for (tp = UnitsTable; tp->name; tp++)
  716.     if (strcmp(buff, tp->name) == 0) {
  717.         yylval.Number = tp->value;
  718.         return tp->type;
  719.     }
  720.  
  721.     /* Strip off any plural and try the units table again. */
  722.     i = strlen(buff) - 1;
  723.     if (buff[i] == 's') {
  724.     buff[i] = '\0';
  725.     for (tp = UnitsTable; tp->name; tp++)
  726.         if (strcmp(buff, tp->name) == 0) {
  727.         yylval.Number = tp->value;
  728.         return tp->type;
  729.         }
  730.     buff[i] = 's';        /* Put back for "this" in OtherTable. */
  731.     }
  732.  
  733.     for (tp = OtherTable; tp->name; tp++)
  734.     if (strcmp(buff, tp->name) == 0) {
  735.         yylval.Number = tp->value;
  736.         return tp->type;
  737.     }
  738.  
  739.     /* Military timezones. */
  740.     if (buff[1] == '\0' && isalpha(*buff)) {
  741.     for (tp = MilitaryTable; tp->name; tp++)
  742.         if (strcmp(buff, tp->name) == 0) {
  743.         yylval.Number = tp->value;
  744.         return tp->type;
  745.         }
  746.     }
  747.  
  748.     /* Drop out any periods and try the timezone table again. */
  749.     for (i = 0, p = q = buff; *q; q++)
  750.     if (*q != '.')
  751.         *p++ = *q;
  752.     else
  753.         i++;
  754.     *p = '\0';
  755.     if (i)
  756.     for (tp = TimezoneTable; tp->name; tp++)
  757.         if (strcmp(buff, tp->name) == 0) {
  758.         yylval.Number = tp->value;
  759.         return tp->type;
  760.         }
  761.  
  762.     return tID;
  763. }
  764.  
  765.  
  766. int
  767. yylex()
  768. {
  769.     register char    c;
  770.     register char    *p;
  771.     char        buff[20];
  772.     int            Count;
  773.     int            sign;
  774.  
  775.     for ( ; ; ) {
  776.     while (isspace(*yyInput))
  777.         yyInput++;
  778.  
  779.     if (isdigit(c = *yyInput) || c == '-' || c == '+') {
  780.         if (c == '-' || c == '+') {
  781.         sign = c == '-' ? -1 : 1;
  782.         if (!isdigit(*++yyInput))
  783.             /* skip the '-' sign */
  784.             continue;
  785.         }
  786.         else
  787.         sign = 0;
  788.         for (yylval.Number = 0; isdigit(c = *yyInput++); )
  789.         yylval.Number = 10 * yylval.Number + c - '0';
  790.         yyInput--;
  791.         if (sign < 0)
  792.         yylval.Number = -yylval.Number;
  793.         return sign ? tSNUMBER : tUNUMBER;
  794.     }
  795.     if (isalpha(c)) {
  796.         for (p = buff; isalpha(c = *yyInput++) || c == '.'; )
  797.         if (p < &buff[sizeof buff - 1])
  798.             *p++ = c;
  799.         *p = '\0';
  800.         yyInput--;
  801.         return LookupWord(buff);
  802.     }
  803.     if (c != '(')
  804.         return *yyInput++;
  805.     Count = 0;
  806.     do {
  807.         c = *yyInput++;
  808.         if (c == '\0')
  809.         return c;
  810.         if (c == '(')
  811.         Count++;
  812.         else if (c == ')')
  813.         Count--;
  814.     } while (Count > 0);
  815.     }
  816. }
  817.  
  818.  
  819. time_t
  820. get_date(p, now)
  821.     char        *p;
  822.     struct timeb    *now;
  823. {
  824.     struct tm        *tm;
  825.     struct timeb    ftz;
  826.     time_t        Start;
  827.     time_t        tod;
  828. #if    defined(FTIME_MISSING)
  829.     extern time_t    timezone;
  830. #endif    
  831.  
  832.     yyInput = p;
  833.     if (now == NULL) {
  834.     now = &ftz;
  835. #if    defined(FTIME_MISSING)
  836.     (void)time(&ftz.time);
  837.     /* Set the timezone global. */
  838.     tzset();
  839.     ftz.timezone = (int) timezone / 60;
  840. #else
  841.     (void)ftime(&ftz);
  842. #endif    /* defined(FTIME_MISSING) */
  843.     }
  844.  
  845.     tm = localtime(&now->time);
  846.     yyYear = tm->tm_year;
  847.     yyMonth = tm->tm_mon + 1;
  848.     yyDay = tm->tm_mday;
  849.     yyTimezone = now->timezone;
  850.     yyDSTmode = DSTmaybe;
  851.     yyHour = 0;
  852.     yyMinutes = 0;
  853.     yySeconds = 0;
  854.     yyMeridian = MER24;
  855.     yyRelSeconds = 0;
  856.     yyRelMonth = 0;
  857.     yyHaveDate = 0;
  858.     yyHaveDay = 0;
  859.     yyHaveRel = 0;
  860.     yyHaveTime = 0;
  861.     yyHaveZone = 0;
  862.  
  863.     if (yyparse()
  864.      || yyHaveTime > 1 || yyHaveZone > 1 || yyHaveDate > 1 || yyHaveDay > 1)
  865.     return -1;
  866.  
  867.     if (yyHaveDate || yyHaveTime || yyHaveDay) {
  868.     Start = Convert(yyMonth, yyDay, yyYear, yyHour, yyMinutes, yySeconds,
  869.             yyMeridian, yyDSTmode);
  870.     if (Start < 0)
  871.         return -1;
  872.     }
  873.     else {
  874.     Start = now->time;
  875.     if (!yyHaveRel)
  876.         Start -= ((tm->tm_hour * 60L + tm->tm_min) * 60L) + tm->tm_sec;
  877.     }
  878.  
  879.     Start += yyRelSeconds;
  880.     Start += RelativeMonth(Start, yyRelMonth);
  881.  
  882.     if (yyHaveDay && !yyHaveDate) {
  883.     tod = RelativeDate(Start, yyDayOrdinal, yyDayNumber);
  884.     Start += tod;
  885.     }
  886.  
  887.     /* Have to do *something* with a legitimate -1 so it's distinguishable
  888.      * from the error return value.  (Alternately could set errno on error.) */
  889.     return Start == -1 ? 0 : Start;
  890. }
  891.  
  892.  
  893. #if    defined(TEST)
  894.  
  895. /* ARGSUSED */
  896. main(ac, av)
  897.     int        ac;
  898.     char    *av[];
  899. {
  900.     char    buff[128];
  901.     time_t    d;
  902.  
  903.     (void)printf("Enter date, or blank line to exit.\n\t> ");
  904.     (void)fflush(stdout);
  905.     while (gets(buff) && buff[0]) {
  906.     d = get_date(buff, (struct timeb *)NULL);
  907.     if (d == -1)
  908.         (void)printf("Bad format - couldn't convert.\n");
  909.     else
  910.         (void)printf("%s", ctime(&d));
  911.     (void)printf("\t> ");
  912.     (void)fflush(stdout);
  913.     }
  914.     exit(0);
  915.     /* NOTREACHED */
  916. }
  917. #endif    /* defined(TEST) */
  918.